﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Sample.Novel.Application.Contracts.Authors;
using Sample.Novel.Domain.Authors.Entities;
using Sample.Novel.Domain.Shared.Localization;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Sample.Novel.Application.Authors
{
    /// <summary>
    /// 作家服务
    /// </summary>
    public class AuthorAppService
        : ApplicationService,IAuthorAppService
    {
        private readonly IRepository<Author,Guid> authorRepository;
        private readonly IStringLocalizer<NovelResource> localizer;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="authorRepository"></param>
    /// <param name="localizer"></param>
        public AuthorAppService(
            IRepository<Author, Guid> authorRepository,
            IStringLocalizer<NovelResource> localizer 
            ) {
            this.authorRepository = authorRepository;
            this.localizer = localizer;

            LocalizationResource = typeof(NovelResource);
        }

        /// <summary>
        /// 获取所有作者
        /// </summary>
        /// <returns>全部作者</returns>
        [HttpGet]
        public async Task<List<AuthorOutput>> GetAuthors() {
            var authors = await authorRepository.GetListAsync();
            return ObjectMapper.Map<List<Author>, List<AuthorOutput>>(authors);
        }

        /// <summary>
        /// 创建新的作者
        /// </summary>
        /// <param name="newAuthor">新的作者</param>
        /// <returns>是否创建成功</returns>
        /// 
        [HttpPost]
        public async Task<bool> NewAuthor(NewAuthorInput newAuthor)
        {
            var author = ObjectMapper.Map<NewAuthorInput,Author>(newAuthor);
            await authorRepository.InsertAsync(author);
            return true;
        }

        /// <summary>
        /// 你好
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string SayHello() {
            return L.GetString("Hello");
        }
    }
}
